package com.authapi.sample.api.controller;

import com.authapi.sample.api.annotation.AdminOnly;
import com.authapi.sample.api.entity.User;
import com.authapi.sample.api.model.Response.Common.Result;
import com.authapi.sample.api.model.Response.Common.ResultList;
import com.authapi.sample.api.model.Response.User.QueryUserRequest;
import com.authapi.sample.api.service.impl.MenuServiceImpl;
import com.authapi.sample.api.service.impl.UserServiceImpl;
import com.authapi.sample.api.utils.PageUtil;
import com.authapi.sample.api.utils.SHA256;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import lombok.var;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.UUID;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Baomidou
 * @since 2024-09-26
 */
@Api(tags = "用户管理") //  tags：你可以当作是这个组的名字。
@RestController

@RequestMapping("/User")

public class UserController {

    @Value("${user.defaultPassword}")
    private String defaultPassword;

    @Autowired
    private UserServiceImpl userService;

    @GetMapping("/GetUser")
    public Result<User> getUser(String id){
        User user = userService.getById(id);
        return Result.Success(user);
    }

    @PostMapping("/AddUser")
    public Result<User> addUser(@RequestBody User user){
        if(user.getId()==null|| user.getId().isEmpty()){
            user.setId(UUID.randomUUID().toString().substring(0,6));
        }
        user.setPassword((SHA256.sha256(defaultPassword)));
        userService.save(user);
        return Result.Success(user);
    }

    @PostMapping("/UpdateUser")
    public Result<User> updateUser(@RequestBody User user){
        userService.updateById(user);
        return Result.Success(user);
    }

    @AdminOnly
    @PostMapping("/DeleteUser")
    public Result<User> deleteUser(@RequestBody User user){
        userService.removeById(user.getId());
        return Result.Success(null);
    }

    @PostMapping("/QueryUser")
    public ResultList<List<User>> QueryUser(@RequestBody QueryUserRequest request){
        int pageIndex = PageUtil.getPageIndexFromHeader();
        int pageSize = PageUtil.getPageSizeFromHeader();
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        if(!request.getName().isEmpty()){
            queryWrapper.eq("name", request.getName());
        }
        if(!request.getPhone().isEmpty()){
            queryWrapper.eq("phone",request.getPhone());
        }
        Page<User> page = new Page<>(pageIndex, pageSize);
        IPage<User> userPage = userService.page(page, queryWrapper);
        var count =(int) userService.count(queryWrapper);
        List<User> result = userPage.getRecords();

        return ResultList.Success(result,count);
    }

    @GetMapping("/ResetPassword")
    public Result<User> ResetPassword(String id){
        User user = userService.getById(id);
        user.setPassword(SHA256.sha256(defaultPassword));
        userService.updateById(user);
        return Result.Success(null);
    }
}
